package com.dianxin.smartpark.service.common.rpc;

import com.chinatelecom.response.common.CommonResponseBody;
import com.chinatelecom.response.constant.ErrMsgEnum;
import com.chinatelecom.response.utils.BeanUtil;
import com.chinatelecom.response.utils.Convert;
import com.chinatelecom.response.utils.ResponseBodyWrapperUtil;
import com.chinatelecom.response.utils.StringUtils;
import com.dianxin.smartpark.api.common.SmartParkUserConfigRpcService;
import com.dianxin.smartpark.api.dto.SmartParkUserConfigDTO;
import com.dianxin.smartpark.dao.dataobject.common.SmartParkUserConfig;
import com.dianxin.smartpark.dao.dataobject.common.SmartParkUserConfigExample;
import com.dianxin.smartpark.dao.mapper.common.SmartParkUserConfigMapper;
import com.dianxin.smartpark.service.common.SmartParkDeviceAuthService;
import com.dianxin.smartpark.service.common.SmartParkUserConfigService;
import com.dianxin.smartpark.service.config.MqttOptType;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Resource;
import java.util.List;

/**
 * 用户凭证业务层处理
 * 
 * @author chenjiasong
 * @date 2021-08-10
 */
@DubboService(version = "1.0.0", group = "smartpark")
public class SmartParkUserConfigRpcServiceImpl implements SmartParkUserConfigRpcService
{
    private final static Logger logger = LoggerFactory.getLogger(SmartParkUserConfigRpcServiceImpl.class);
    @Resource
    private SmartParkUserConfigMapper smartParkUserConfigMapper;

    @Resource
    private SmartParkDeviceAuthService smartParkDeviceAuthService;

    /**
     * 新增用户凭证
     * 
     * @param vo 用户凭证
     * @return 结果
     */
    @Override
    public CommonResponseBody<Boolean> insertSmartParkUserConfig(SmartParkUserConfigDTO vo)
    {
        // 验证 todo
        SmartParkUserConfigExample smartParkUserConfigExample = new SmartParkUserConfigExample();
        SmartParkUserConfigExample.Criteria criteria = smartParkUserConfigExample.createCriteria();
        if (vo.getTenantId() != null) {
            criteria.andTenantIdEqualTo(vo.getTenantId());
        }
        criteria.andIsDeletedEqualTo(0);
        criteria.andUserIdEqualTo(vo.getUserId());
        List<SmartParkUserConfig> passRecords = smartParkUserConfigMapper.selectByExample(smartParkUserConfigExample);
        if(StringUtils.isNotEmpty(passRecords)){
            return ResponseBodyWrapperUtil.fail("用户凭证已存在","400");
        }
        SmartParkUserConfig smartParkUserConfig = new SmartParkUserConfig();
        try {
            BeanUtil.insertConversion(vo, smartParkUserConfig);
        } catch (Exception e) {
            logger.error("", e);
        }
        smartParkUserConfig.beforeInsert();
        smartParkUserConfig.setUserId(Convert.toStr(vo.getUserId()));
        return ResponseBodyWrapperUtil.toResData(smartParkUserConfigMapper.insertSelective(smartParkUserConfig));
    }

    /**
     * 修改用户凭证
     * 
     * @param vo 用户凭证
     * @return 结果
     */
    @Override
    public CommonResponseBody<Boolean> updateSmartParkUserConfig(SmartParkUserConfigDTO vo)
    {
        if(StringUtils.isNull(vo.getUserId())){
            return ResponseBodyWrapperUtil.fail(ErrMsgEnum.ERROR_PARAME);
        }
        // 验证 todo
        SmartParkUserConfig smartParkUserConfig = new SmartParkUserConfig();
        try {
            BeanUtil.insertConversion(vo, smartParkUserConfig);
        } catch (Exception e) {
            logger.error("", e);
        }
        smartParkUserConfig.beforeUpdate();
        smartParkUserConfig.setUserId(Convert.toStr(vo.getUserId()));
        return ResponseBodyWrapperUtil.toResData(smartParkUserConfigMapper.updateByPrimaryKeySelective(smartParkUserConfig));
    }

    /**
     * 删除用户凭证信息
     * 
     * @param vo 用户凭证
     * @return 结果08
     */
    @Override
    public CommonResponseBody<Boolean> deleteSmartParkUserConfig(SmartParkUserConfigDTO vo){
        // 验证 todo
        if(StringUtils.isNull(vo.getUserId())){
            return ResponseBodyWrapperUtil.fail(ErrMsgEnum.ERROR_PARAME);
        }
        SmartParkUserConfig smartParkUserConfig = new SmartParkUserConfig();
        int res = 0;
        try {
            BeanUtil.insertConversion(vo, smartParkUserConfig);
            smartParkUserConfig.beforeDelete();
            smartParkUserConfig.setUserId(Convert.toStr(vo.getUserId()));
            res = smartParkUserConfigMapper.updateByPrimaryKeySelective(smartParkUserConfig);
            if(res>0){
                smartParkDeviceAuthService.sendUserDeviceDelAuth(Convert.toStr(vo.getUserId()),MqttOptType.DELUSERFACE.getOptCode(),null);
            }
        } catch (Exception e) {
            logger.error("", e);
        }
        return ResponseBodyWrapperUtil.toResData(res);
    }
}
